/**
 * Created by user on 2015/9/28.
 */

angular.module('mis.regionsManager.controllers', [])
    .controller('regionsCtrl', ['$scope','$http', '$location', function ($scope, $http, $location) {


        $scope.currentPage = 1;
        $scope.pageSize = 15;

        //行政区域分页
        $scope.pagination = new pagination({currentPage:$scope.currentPage,pageSize: $scope.pageSize,
            url:'http://' + backOfficeHost + '/rest/v1.0/mis/master/regionsList/:page/:size',
            ajax: $http,
            success:function(data){
                $scope.regionsList = data.data.list;
                $scope.pageNumber = data.data.totalPage;
                $scope.count = data.data.count;
                $scope.currentPage = $(".am-active").text();
                $scope.pagination.totalPage = data.data.totalPage;
                $scope.pagination.pageHeader($scope.pageNumber);
                $('#count').text("共"+$scope.pageNumber+"页 有"+$scope.count+"数据");
            },
            error: function(err){
                //$scope.communityList = null;
            },
            params: {}
        });

        //级联框
        var cascadData = {
            provinceUrl: 'http://' + backOfficeHost + '/rest/v1.0/backoffice/region/regionPrivince',
            cityUrl: 'http://' + backOfficeHost + '/rest/v1.0/backoffice/region/regionCity',
            cityAreaUrl: 'http://' + backOfficeHost + '/rest/v1.0/backoffice/region/regionCounty',
            villageUrl: 'http://' + backOfficeHost + '/rest/v1.0/mis/community/communityList',
            ajax: $http
        };
        $scope.cascading = new gObj.cascading(cascadData);
        $scope.cascading.paint($(".city-cascading")[0],false);

        //搜索
        $scope.search = function (current) {

            if (current) {
                $scope.currentPage = $scope.pagination.currentPage;
            }else {
                $scope.pagination.currentPage = 1;
                $scope.currentPage = 1;
            }

            //var community = $scope.cascading.getVillageId(document.getElementById("search-target"));

            var parentId =null;
            var body = {};

            if($scope.cascading.getProvince(document.getElementById("search-target"))){
                parentId = $scope.cascading.getProvince(document.getElementById("search-target"));
            };

            if($scope.cascading.getCity(document.getElementById("search-target"))){
                parentId = $scope.cascading.getCity(document.getElementById("search-target"));
            };

            if (parentId) body.parentId = parentId;

            $http.post('http://' + backOfficeHost + '/rest/v1.0/mis/master/regionsList/' + $scope.currentPage + '/' + $scope.pageSize, body, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                console.debug(data.data);
                $scope.regionsList = data.data.list;
                $scope.pageNumber = data.data.totalPage;
                $scope.count = data.data.count;

                $scope.pagination.totalPage = data.data.totalPage;
                $scope.pagination.pageHeader($scope.pageNumber);
                $scope.pagination.searchParams = body;
            }).error(function (err) {
                alert(err);
            });

        };

        //初始化
        $scope.countyInit = function(region){

            $http.post('http://' + backOfficeHost + '/rest/v1.0/mis/master/creatRegionByCity', {name:region.name}, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {

                if(data.result.status == 0){
                    alert(data.result.message);
                }else {
                    alert("初始化成功");
                }
                $scope.search(true);
                $('#doc-modal-add').modal("close");

            }).error(function (err) {
                alert(err);
            });

        };

    }])